package com.laboratory.controller;

import com.laboratory.domain.ResponseCode;
import com.laboratory.domain.ResponseResult;
import com.laboratory.domain.dto.OrderDto;
import com.laboratory.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * ConstraintViolationException （方法参数校验失败）
     */
    @GetMapping("/create")
    public ResponseResult createOrder(@RequestParam String orderId, @RequestParam int quantity) {
        return new ResponseResult(
                ResponseCode.GET_SUCCESS,
                "创建订单成功",
                orderService.placeOrder(orderId, quantity)
        );
    }

    /**
     * 创建订单
     *
     * @param orderDto 请求体里的订单信息
     * @return ResponseResult
     */
    @PostMapping
    public ResponseResult createOrder(@Validated @RequestBody OrderDto orderDto) {
        /**
         * 如果不触发校验异常，就会返回成功
         */
        return new ResponseResult(
                ResponseCode.GET_SUCCESS,
                "订单创建成功",
                orderDto
        );
    }

}
